package awk.script.javassist.field02;

import com.alibaba.ttl.TransmittableThreadLocal;
import lombok.Getter;
import lombok.Setter;

import java.util.HashMap;
import java.util.Map;

/**
 * @author yujianlong
 * @create 2021-03-21 14:06
 **/

@Getter
@Setter
public class ZTContHolder {
////	private static final ThreadLocal<HashMap<String, String>> context = new ThreadLocal<>();
//	private static final TransmittableThreadLocal<HashMap<String, String>> context = new TransmittableThreadLocal<>();
//
//	public static void init() {
//		ZTContHolder.clear();
//		ZTContHolder.context.set(new HashMap<>());
//	}
//
//	public static void clear() {
//		HashMap<String, String> tmp = ZTContHolder.context.get();
//		if (tmp != null) {
//			ZTContHolder.context.set(null);
//			tmp.clear();
//		}
//	}
//
//	public static HashMap<String, String> data() {
//		HashMap<String, String> tmp = ZTContHolder.context.get();
//		return (null == tmp) ? new HashMap<>() : tmp;
//	}
//
//	public static Object get(String key) {
//		if (null == ZTContHolder.context.get()) {
//			ZTAPIContextHolder.init();
//		}
//		return ZTContHolder.context.get().get(key);
//	}
//
//	public static Object getOrDefault(String key, Object defaultValue) {
//		Object rtn = ZTAPIContextHolder.get(key);
//		return rtn == null ? defaultValue : rtn;
//	}
//
//	public static void set(Map<String, String> entries) {
//		ZTContHolder.context.get().putAll(entries);
//	}
//
//	public static void set(String key, Object value) {
//		ZTContHolder.context.get().put(key, String.valueOf(value));
//	}



	private static final ThreadLocal<HashMap<String, String>> context = new ThreadLocal();
	private static final TransmittableThreadLocal<HashMap<String, String>> newContext = new TransmittableThreadLocal();

	public ZTContHolder() {
	}

	public static void init() {
		clear();
		newContext.set(new HashMap());
	}

	public static void clear() {
		HashMap var0 = (HashMap)context.get();
		if (var0 != null) {
			newContext.set(null);
			var0.clear();
		}

	}

	public static HashMap<String, String> data() {
		HashMap var0 = (HashMap)newContext.get();
		return null == var0 ? new HashMap() : var0;
	}

	public static Object get(String var0) {
		if (null == newContext.get()) {
			init();
		}

		return ((HashMap)newContext.get()).get(var0);
	}

	public static Object getOrDefault(String var0, Object var1) {
		Object var2 = get(var0);
		return var2 == null ? var1 : var2;
	}

	public static void set(Map<String, String> var0) {
		((HashMap)newContext.get()).putAll(var0);
	}

	public static void set(String var0, Object var1) {
		((HashMap)newContext.get()).put(var0, String.valueOf(var1));
	}
}
